package com.zjw.core.algorithm;

public class BigNumberMultiply {

    public static void main(String[] args) {
        String x = "237669";
        String y = "1477";
        System.out.println(237669*1477);
        char[] large = null;
        char[] small = null;
        //比较两数大小，大的赋值为large，小的为small
        if(x.length() > y.length()){
            large = x.toCharArray();
            small = y.toCharArray();
        }else if(x.length() < y.length()){
            large = y.toCharArray();
            small = x.toCharArray();
        }else {
            if(Integer.valueOf(x) > Integer.valueOf(y)){
                large = x.toCharArray();
                small = y.toCharArray();
            }else {
                large = y.toCharArray();
                small = x.toCharArray();
            }
        }
        //两数乘积最大位数
        int[] multiply = new int[large.length + small.length];
        for (int i = small.length - 1; i >= 0; i--) {
            for (int j = large.length - 1; j >= 0; j--) {
                int smallNum = small[i] - '0';
                int largeNum = large[j] - '0';
                multiply[large.length - i -1 + small.length - j -1]  += smallNum * largeNum;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < multiply.length; i++) {
            if(multiply[i] > 9){
                multiply[i+1] = multiply[i+1] + multiply[i] / 10;
                multiply[i] = multiply[i] %10;
            }
            stringBuilder.append(multiply[i]);
        }
        String result = stringBuilder.reverse().toString();
        if(stringBuilder.toString().startsWith("0")){
            result = stringBuilder.substring(1);
        }
        System.out.println(result);
    }
}
